package cn.jimmie.learn.viewArt.weight

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.view.View

/**
 * function :
 *
 * <p> Create Time: 2019年01月15日 22:51 </p>
 * <p> @author Jimmie.Qian  </p>
 */

class LrcView(context: Context, attrs: AttributeSet) : BaseView(context, attrs) {
    var text = "hello world, hello android"
        set(value) {
            field = value
            pct = 0f
        }

    var pct = 0f
        set(value) {
            field = value
            invalidate()
        }

    var type = TYPE_CLIP
        set(value) {
            field = value
            invalidate()
        }

    var lrcColor = Color.YELLOW
    var lrcHighLight = Color.WHITE

    init {
        paint.textSize = 20 * sp
        paint.textAlign = Paint.Align.CENTER
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onDraw(canvas: Canvas) {
        when (type) {
            TYPE_CLIP -> drawLrcTextClip(canvas)
            TYPE_LAYER -> drawLrcTextLayer(canvas)
            TYPE_NONE -> drawLrcWithoutOverdraw(canvas)
        }
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun drawLrcTextLayer(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f

        // 中间写文字
        val fm = paint.fontMetrics
        val baseLine = halfH + (fm.descent - fm.ascent) / 2
        paint.color = lrcColor
        canvas.drawText(text, halfW, baseLine, paint)

        // 开启硬件加速
        setLayerType(View.LAYER_TYPE_HARDWARE, null)
        val count = canvas.saveLayer(0f, 0f, width * pct, height.toFloat(), paint)
        paint.color = lrcHighLight
        canvas.drawText(text, halfW, baseLine, paint)
        canvas.restoreToCount(count)
        // 关闭硬件加速
        setLayerType(View.LAYER_TYPE_NONE, null)
    }

    private fun drawLrcTextClip(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f
        // 中间写文字
        val fm = paint.fontMetrics
        val baseLine = halfH + (fm.descent - fm.ascent) / 2
        paint.color = lrcColor
        canvas.drawText(text, halfW, baseLine, paint)

        canvas.clipRect(0f, 0f, width * pct, height.toFloat())
        paint.color = lrcHighLight
        canvas.drawText(text, halfW, baseLine, paint)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun drawLrcWithoutOverdraw(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f
        // 中间写文字
        val fm = paint.fontMetrics
        val baseLine = halfH + (fm.descent - fm.ascent) / 2
        paint.color = lrcColor

        canvas.save()
        canvas.clipOutRect(0f, 0f, width * pct, height.toFloat())
        canvas.drawText(text, halfW, baseLine, paint)
        canvas.restore()

        canvas.clipRect(0f, 0f, width * pct, height.toFloat())
        paint.color = lrcHighLight
        canvas.drawText(text, halfW, baseLine, paint)
    }

    companion object {
        const val TYPE_CLIP = 1
        const val TYPE_LAYER = 2
        const val TYPE_NONE = 3
    }
}